package com.smarttest.portal.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarttest.common.beans.JsonDataResult;
import com.smarttest.portal.beans.InterfaceDetail;
import com.smarttest.portal.pojo.DbAutoLocalinterface;
import com.smarttest.portal.service.AddInterfaceService;
import com.smarttest.portal.service.OpenAPIService;

@Controller
public class OpenAPIController {
	@Autowired
	private OpenAPIService openAPIService;
	@Autowired
	private AddInterfaceService addInterfaceService;
	
	@RequestMapping(value = "/openapi/searchInfs.action", method = { RequestMethod.POST,
			RequestMethod.GET }, produces = "application/json; charset=utf-8")
	@ResponseBody
	public JsonDataResult searchInterface(String searchValue) {
		List<Object> l = new ArrayList<Object>();
		List<InterfaceDetail> results = openAPIService.queryInterface(searchValue);
		l.addAll(results);
		List<DbAutoLocalinterface> localresults = addInterfaceService.getLocalInterface();
		l.addAll(localresults);
		/*if(null!=l&&l.size()>0){
		return JsonDataResult.ok(l);
		}else{
		return JsonDataResult.error(400);
		}*/
		return JsonDataResult.ok(l);
	}
	
	@RequestMapping(value = "/openapi/searchInfDetail.action", method = { RequestMethod.POST,
			RequestMethod.GET }, produces = "application/json; charset=utf-8")
	@ResponseBody
	public JsonDataResult searchInterfaceDetail(int cid,String defined) {
		if(cid<=0){
			return JsonDataResult.error(400);
		}
		List<Object> l = new ArrayList<Object>();
		if (!defined.equalsIgnoreCase("self")) {
			List<InterfaceDetail> results = openAPIService.queryInterfaceDetail(cid);
			l.addAll(results);
		}
		else {
			List<DbAutoLocalinterface> localresults = addInterfaceService.getLocalInterfaceDetails(cid);
			l.addAll(localresults);
		}		
		if(null!=l&&l.size()>0){
		return JsonDataResult.ok(l);
		}else{
		return JsonDataResult.error(400);
		}
	}
}
